package com.test.mooan.labama.dialog_boom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.test.mooan.labama.R;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;


public class DeleteDataDialog_boom extends Dialog {

    private final String pageTag = "DIALOG_DELETE_DATA";

    public DeleteDataDialog_boom(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_data_boom);
        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        Button returnBtn = findViewById(R.id.delete_data_btn_return);
        Button confirmBtn = findViewById(R.id.delete_data_btn_confirm);

        returnBtn.setOnClickListener(v -> {
            UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_RETURN_CLICK");
            dismiss();
        });

        new Thread(() -> {

            int count = 10;

            while (count-->0 && isShowing()){

                if (isShowing()){
                    final int finalCount = count;
                    Threadutils_boom.getInstance().runOnUiThread(() -> {
                        confirmBtn.setText(getContext().getString(R.string.confirm)+"("+finalCount+"s)");
                    });
                }

                try {
                    Thread.sleep(999);
                }catch (InterruptedException e){
                    if (Constant_boom.Debug) e.printStackTrace();
                }
            }

            if (isShowing())
                Threadutils_boom.getInstance().runOnUiThread(() -> {
                    confirmBtn.setAlpha(1f);
                    confirmBtn.setText(getContext().getString(R.string.confirm));
                    confirmBtn.setOnClickListener(v -> {
                        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_CONFIRM_CLICK");
                        dismiss();
                        HttpManager_boom.getInstance().deleteUserData();
                    });
                });

        }).start();

    }

    @Override
    public void show() {
        super.show();
        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_START");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        UserInfoHelper_boom.gInstance().addEventPoint(pageTag+"_END");
    }

}
